// 就餐记录
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

const classStudent: Record<string, string[]> = {
    '一班': [
        '张伟', '李娜', '王强', '刘洋', '陈晨',
        '赵敏', '黄磊', '周婷', '吴昊', '徐帆',
        '孙莉', '胡军', '郭涛', '何洁', '林峰',
        '郑凯', '谢娜', '冯绍', '程琳', '邓超'
    ],
    '二班': [
        '李明', '王芳', '刘杰', '陈静', '杨光',
        '黄伟', '周丽', '吴磊', '徐倩', '孙浩',
        '胡雪', '郭佳', '何勇', '林娜', '郑宇',
        '谢军', '冯莉', '程浩', '邓婷', '曾志'
    ],
    '三班': [
        '王磊', '李婷', '刘畅', '陈浩', '张敏',
        '赵阳', '黄静', '周伟', '吴芳', '徐磊',
        '孙倩', '胡佳', '郭明', '何涛', '林雪',
        '郑莉', '谢浩', '冯佳', '程峰', '邓丽'
    ]
}

// 班级数据
interface ClassData {
    className: string
    diningNames: string[]
}

export const useDiningStore = defineStore('dining', () => {
    // 所有班级数据
    const classes = ref<ClassData[]>([
        {
            className: '一班', diningNames: ['张伟', '李娜', '王强', '刘洋', '陈晨',
                '赵敏', '黄磊', '周婷', '吴昊', '徐帆',
                '孙莉', '胡军', '郭涛', '何洁', '林峰',
                '郑凯', '谢娜', '冯绍', '程琳', '邓超']
        },
        {
            className: '二班', diningNames: ['李明', '王芳', '刘杰', '陈静', '杨光',
                '黄伟', '周丽', '吴磊', '徐倩', '孙浩',
                '胡雪', '郭佳', '何勇', '林娜', '郑宇',]
        },
        { className: '三班', diningNames: [] },
    ])


    const currentClass = ref<string>('一班')

    // 当前班级数据
    const currentClassData = computed(() => {
        return classes.value.find(c => c.className === currentClass.value)!
    })


    const totalStudents = computed(() => 20)

    // 当前班级所有学生姓名
    const allStudentsInCurrentClass = computed(() => {
        return classStudent[currentClass.value] || []
    })

    // 就餐人数
    const diningCount = computed(() => currentClassData.value.diningNames.length)

    // 未就餐人数
    const notDiningCount = computed(() => totalStudents.value - diningCount.value)

    // 未就餐学生名单
    const notDiningNames = computed(() => {
        return allStudentsInCurrentClass.value.filter(
            name => !currentClassData.value.diningNames.includes(name)
        )
    })

    // 是否未就餐人数超过5人
    const hasExceededLimit = computed(() => notDiningCount.value > 5)

    // 所有班级名称列表
    const classList = computed(() => Object.keys(classStudent))

    // 切换班级
    const setCurrentClass = (className: string) => {
        if (classList.value.includes(className)) {
            currentClass.value = className
        }
    }


    return {
        currentClass,
        classList,
        totalStudents,
        diningCount,
        notDiningCount,
        diningNames: computed(() => [...currentClassData.value.diningNames]),
        notDiningNames,
        hasExceededLimit,
        setCurrentClass,

    }
})